package org.jeecg.common.util;

import io.netty.util.internal.StringUtil;


public class CodeUtil {


	private static int numLength = 3;//代表数字位数


	public static synchronized String getNextCode(String code,String prefix,int len) {
		numLength=len;
		String newcode = "";
		if (oConvertUtils.isEmpty(code)) {
			String num = getStrNum(1);
			newcode = prefix+num;
		} else {

			String after_code = code.replaceAll("\\D+", "");//仅仅保留数字
			Integer after_code_num = Integer.parseInt(after_code);
		

			String nextNum = "";
	
			// 先判断数字等于999*，则计数从1重新开始，递增
			if (after_code_num == getMaxNumByLength(numLength)) {
				//nextNum = getNextStrNum(0);
				numLength+=1; //当超过最大长度时候，最大长度自动加一
			}
			nextNum = getNextStrNum(after_code_num);

		    newcode = prefix + nextNum;
			
		}
		return newcode;

	}
	
	public static synchronized String getNextCode(String code,String prefix,String styleCateCode,String dateString ,int len) {
		numLength=len;
		String newcode = "";
		if (oConvertUtils.isEmpty(code)) {
			String num = getStrNum(1);
			newcode = prefix+num;
		} else {

			String after_code = code.replaceAll("\\D+", "");//仅仅保留数字
			after_code=after_code.substring(4,after_code.length()); //删除前四位 日期数字，保留流水号
			
			Integer after_code_num = Integer.parseInt(after_code);
		
		
			String nextNum = "";
	
//			// 先判断数字等于999*，则计数从1重新开始，递增
			if (after_code_num == getMaxNumByLength(numLength)) {
				//nextNum = getNextStrNum(0);
				numLength+=1; //当超过最大长度时候，最大长度自动加一
			}
			nextNum = getNextStrNum(after_code_num);

		    newcode = prefix +styleCateCode+dateString+ nextNum;
			
		}
		return newcode;

	}
	

//	/**
//	 * 根据父亲code,获取下级的下一个code
//	 * 
//	 * 例如：父亲CODE:A01
//	 *       当前CODE:A01B03
//	 *       获取的code:A01B04
//	 *       
//	 * @param parentCode   上级code
//	 * @param localCode    同级code
//	 * @return
//	 */
//	public static synchronized String getSubCode(String parentCode,String localCode) {
//		if(localCode!=null && localCode!=""){
//
////			return parentCode + getNextCode(localCode);
//			return getNextCode(localCode);
//
//		}else{
//			parentCode = parentCode + "A"+ getNextStrNum(0);
//		}
//		return parentCode;
//	}
//
//	

	/**
	 * 将数字前面位数补零
	 * 
	 * @param num
	 * @return
	 */
	private static String getNextStrNum(int num) {
		return getStrNum(getNextNum(num));
	}

	/**
	 * 将数字前面位数补零
	 * 
	 * @param num
	 * @return
	 */
	private static String getStrNum(int num) {
		String s = String.format("%0" + numLength + "d", num);
		return s;
	}

	/**
	 * 递增获取下个数字
	 * 
	 * @param num
	 * @return
	 */
	private static int getNextNum(int num) {
		num++;
		return num;
	}

	/**
	 * 递增获取下个字母
	 * 
	 * @param num
	 * @return
	 */
	private static char getNextZiMu(char zimu) {
		if (zimu == 'Z') {
			return 'A';
		}
		zimu++;
		return zimu;
	}
	
	/**
	 * 根据数字位数获取最大值
	 * @param length
	 * @return
	 */
	private static int getMaxNumByLength(int length){
		if(length==0){
			return 0;
		}
		String max_num = "";
		for (int i=0;i<length;i++){
			max_num = max_num + "9";
		}
		return Integer.parseInt(max_num);
	}
	public static String[] cutCode(String code){
		if(code==null || StringUtil.isNullOrEmpty(code)){
			return null;
		}else{
			//获取标准长度为numLength+1,截取的数量为code.length/numLength+1
			int c = code.length()/(numLength+1);
			String[] cutcode = new String[c];
			for(int i =0 ; i <c;i++){
				cutcode[i] = code.substring(0,(i+1)*(numLength+1));
			}
			return cutcode;
		}
		
	}
//	public static void main(String[] args) {
//		// org.jeecgframework.core.util.LogUtil.info(getNextZiMu('C'));
//		// org.jeecgframework.core.util.LogUtil.info(getNextNum(8));
//	    // org.jeecgframework.core.util.LogUtil.info(cutCode("C99A01B01")[2]);
//	}
}
